package com.buddies.lottery.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.buddies.lottery.R;
import com.buddies.lottery.utils.LotteryUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class DotView extends LinearLayout {

    private final int MAX_DOT_COUNT = 10;

    private final int mDefaultSize = ConvertUtils.dp2px(24);

    private int mDotBackground;
    private int mDotPadding = ConvertUtils.dp2px(4);
    private int mDotWidth = mDefaultSize;
    private int mDotHeight = mDefaultSize;
    private int mDotTextSize = ConvertUtils.sp2px(12);
    private int mDotInnerPadding = ConvertUtils.dp2px(1);
    private int mDotTextColor = Color.WHITE;
    private Map<String, Integer> mColorNumMap;

    public DotView(Context context) {
        this(context, null);
    }

    public DotView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DotView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DotView, defStyleAttr, 0);
        mDotWidth = (int) ta.getDimensionPixelSize(R.styleable.DotView_dotWidth, mDotWidth);
        mDotHeight = (int) ta.getDimensionPixelSize(R.styleable.DotView_dotHeight, mDotHeight);
        mDotPadding = (int) ta.getDimensionPixelSize(R.styleable.DotView_dotPadding, mDotPadding);
        mDotInnerPadding = (int) ta.getDimensionPixelSize(R.styleable.DotView_dotInnerPadding, mDotInnerPadding);
        mDotTextSize = (int) ta.getDimensionPixelSize(R.styleable.DotView_dotTextSize, mDotTextSize);
        mDotTextColor = ta.getColor(R.styleable.DotView_dotTextColor, mDotTextColor);
        mDotBackground = ta.getResourceId(R.styleable.DotView_dotBackground, mDotBackground);
        ta.recycle();
    }

    public void setDots(int lotteryId, String[] dots) {
        setDotNumColors(LotteryUtils.getLotteryColorNumMap(lotteryId));
        setDots(dots);
    }

    public void setDots(String[] dots) {
        setDots(dots != null ? Arrays.asList(dots) : null);
    }

    public void setDots(List<String> dots) {
        removeAllViews();

        int dotCount = dots != null ? dots.size() : 0;
        if (dotCount > MAX_DOT_COUNT) {
            dotCount = MAX_DOT_COUNT;
        }

        int dotWidth = mDotWidth;
        int dotHeight = mDotHeight;
        int dotPadding = mDotPadding;

        if (dotCount == MAX_DOT_COUNT && dotWidth >= mDefaultSize) {
            dotWidth = mDefaultSize - ConvertUtils.dp2px(4);
            dotHeight = dotWidth;
            dotPadding = ConvertUtils.dp2px(3);
        }

        for (int i = 0; i < dotCount; i++) {
            String dot = dots.get(i);

            TextView text = new TextView(getContext());

            int background = mDotBackground;
            if (mColorNumMap != null && !mColorNumMap.isEmpty()) {
                int numColor = LotteryUtils.getNumColor(dot, mColorNumMap);
                if (numColor != 0) {
                    background = numColor;
                }
            }
            text.setBackgroundResource(background);
//            text.setTextSize(mDotTextSize);
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDotTextSize);
            text.setTextColor(mDotTextColor);
            text.setText(dot);
            text.setSingleLine();
            text.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            text.setSelected(true);
            text.setGravity(Gravity.CENTER);
            text.setMinHeight(dotHeight);
            text.setMinWidth(dotWidth);
            text.setPadding(mDotInnerPadding, 0, mDotInnerPadding, 0);
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = i != 0 ? dotPadding : 0;
            addView(text, lp);
        }
    }

    public void setDotBackground(@IdRes int resId) {
        mDotBackground = resId;
    }

    public void setDotTextSize(int px) {
        mDotTextSize = px;
    }

    public void setDotTextColor(@ColorRes int color) {
        mDotTextColor = color;
    }

    public void setDotPadding(int px) {
        mDotPadding = px;
    }

    public void setDotWidth(int px) {
        mDotWidth = px;
    }

    public void setDotHeight(int px) {
        mDotHeight = px;
    }

    public void setDotNumColors(Map<String, Integer> colorNumMap) {
        mColorNumMap = colorNumMap;
    }

}
